package com.audi.view;

import java.util.ArrayList;

import org.json.JSONObject;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.audi.R;
import com.audi.dto.DeviceItem;
import com.audi.utils.Constants;

public class EditDayDialog {
	private Button mBtnEditDayOk, mBtnEditDayCancel;
	private LinearLayout mLnlSun, mLnlMon, mLnlTue;
	private LinearLayout  mLnlWed, mLnlThu, mLnlFri, mLnlSat;
	private ImageView mImgSun, mImgMon, mImgTue;
	private ImageView mImgWed, mImgThu, mImgFri, mImgSat;
	private Spinner mSpnMinutes, mSpnHours, mSpnAmPm;
	private String[] mListMinutes = { "00", "15", "30", "45" };
	private String[] mListHours = { "01", "02", "03", "04", "05", "06", "07",
			"08", "09", "10", "11", "12" };
	private String[] mListAmPm = { "AM", "PM" };
	private ArrayAdapter<String> mAdapterSpinnerMinutes, mAdapterSpinnerHours,
			mAdapterSpinnerAmPm;
	private ImageButton mIbtnMinutes, mIbtnHour;
	private Context context;
	private AudiDialog dialogEditDay;
	private String strHours, strAmOrPm, strMinutes;
	private ArrayList<String> strDay=new ArrayList<String>();
	DeviceItem deviceItem;
	int Valuastatus;

	public EditDayDialog(Context activity) {
		this.context = activity;
	}

	public void dialogEditDayOfWeek() {
		
		dialogEditDay = new AudiDialog(context);
		dialogEditDay.setLayout(R.layout.dialog_edit_days);
		dialogEditDay.show();
		getWidget();
		addEventForWidget();
	}
	private void getWidget() {
		mBtnEditDayCancel = (Button) dialogEditDay
				.findViewById(R.id.btn_edit_day_cancel);
		mBtnEditDayOk = (Button) dialogEditDay
				.findViewById(R.id.btn_edit_day_ok);
		mLnlSun = (LinearLayout) dialogEditDay.findViewById(R.id.lnl_sun);
		mImgSun= (ImageView) dialogEditDay.findViewById(R.id.img_sun);
		mLnlMon = (LinearLayout) dialogEditDay.findViewById(R.id.lnl_mon);
		mImgMon= (ImageView) dialogEditDay.findViewById(R.id.img_mon);
		mLnlTue = (LinearLayout) dialogEditDay.findViewById(R.id.lnl_tue);
		mImgTue= (ImageView) dialogEditDay.findViewById(R.id.img_tue);
		mLnlWed = (LinearLayout) dialogEditDay.findViewById(R.id.lnl_wed);
		mImgWed= (ImageView) dialogEditDay.findViewById(R.id.img_wed);
		mLnlThu = (LinearLayout) dialogEditDay.findViewById(R.id.lnl_thu);
		mImgThu= (ImageView) dialogEditDay.findViewById(R.id.img_thu);
		mLnlFri = (LinearLayout) dialogEditDay.findViewById(R.id.lnl_fri);
		mImgFri= (ImageView) dialogEditDay.findViewById(R.id.img_fri);
		mLnlSat = (LinearLayout) dialogEditDay.findViewById(R.id.lnl_sat);
		mImgSat= (ImageView) dialogEditDay.findViewById(R.id.img_sat);
		mSpnHours = (Spinner) dialogEditDay
				.findViewById(R.id.spn_hour_edit_day);
		mSpnMinutes = (Spinner) dialogEditDay
				.findViewById(R.id.spn_minutes_edit_day);
		mSpnAmPm = (Spinner) dialogEditDay.findViewById(R.id.spn_am_or_pm);
		mIbtnHour = (ImageButton) dialogEditDay
				.findViewById(R.id.img_hour_edit_day);
		mIbtnMinutes = (ImageButton) dialogEditDay
				.findViewById(R.id.img_minutes_edit_day);
		mAdapterSpinnerHours = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item, mListHours);
		mAdapterSpinnerHours
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpnHours.setAdapter(mAdapterSpinnerHours);
		mAdapterSpinnerMinutes = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item, mListMinutes);
		mAdapterSpinnerMinutes
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpnMinutes.setAdapter(mAdapterSpinnerMinutes);
		mAdapterSpinnerAmPm = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item, mListAmPm);
		mAdapterSpinnerAmPm
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpnAmPm.setAdapter(mAdapterSpinnerAmPm);
	}

	private void addEventForWidget() {
		onClickForLinerLayout(mLnlMon, mImgMon, "Monday");
		onClickForLinerLayout(mLnlTue, mImgTue, "Tuesday");
		onClickForLinerLayout(mLnlWed, mImgWed, "Wednesday");
		onClickForLinerLayout(mLnlThu, mImgThu, "Thursday");
		onClickForLinerLayout(mLnlFri, mImgFri, "Friday");
		onClickForLinerLayout(mLnlSat, mImgSat, "Saturday");
		onClickForLinerLayout(mLnlSun, mImgSun, "Sunday");
		mIbtnHour.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mSpnHours.performClick();
				mSpnHours
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								strHours = mListHours[arg2];
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								strHours = "";
							}
						});
			}
		});

		mSpnAmPm.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				strAmOrPm = mListAmPm[arg2];
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				strAmOrPm = "";
			}
		});
		mIbtnMinutes.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mSpnMinutes.performClick();
				mSpnMinutes
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								strMinutes = mListMinutes[arg2];

							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								strMinutes = "";
							}
						});
			}
		});
		mBtnEditDayCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Toast.makeText(context, "Back about 5-3-3-1",
						Toast.LENGTH_SHORT).show();
				dialogEditDay.dismiss();
			}
		});
		mBtnEditDayOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Toast.makeText(
						context,
						"Continue 5-3-2-5: " + "Hours - " + strHours
								+ strAmOrPm + ", Minutes - " + strMinutes
								+ ", Day - " + strDay, Toast.LENGTH_LONG)
						.show();

				String vRet = "Continue 5-3-2-5: " + "Hours - " + strHours
						+ strAmOrPm + ", Minutes - " + strMinutes + ", Day - "
						+ strDay;
				Toast.makeText(context, vRet, Toast.LENGTH_LONG)

				.show();
				
//				AddActionCardActivity activity = (AddActionCardActivity) context;
//				activity.setDataFor(vRet);
//				activity.chonTrigger(deviceItem, Valuastatus);
				dialogEditDay.dismiss();
			}
		});
	}

	private void onClickForLinerLayout(LinearLayout mLnl, ImageView img, String str){
		final ImageView mImg=img;
		final String mStr=str;
		mLnl.setOnClickListener(new OnClickListener() {
			Boolean flag= false;
			@Override
			public void onClick(View v) {
				if(!flag){
					mImg.setImageResource(R.drawable.btn_dash_blue);
					strDay.add(mStr);
					flag=true;
				} else{
					mImg.setImageResource(R.drawable.btn_dash_grey);
					strDay.remove(mStr);
					flag=false;
				}
			}
		});
	}
}
